package monster.com.cvh.fragment.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import monster.com.cvh.R;
import monster.com.cvh.listener.OnHideKeyBoardListener;
import monster.com.cvh.util.KeyboardUtils;

/**
 * @author: pengguanming
 * @data: 2018/4/23
 * @function:用户信息保存状态监听Fragment
 */
public abstract class ResumeInfoBaseFragment extends PermissionFragment implements OnHideKeyBoardListener {
    protected ProgressDialog mDialog;//网络请求ProgressDialog
    protected AlertDialog mAlertDialog;//提示保存信息AlertDialog
    private static final String TAG = "ResumeInfoBaseFragment";

    protected <T> List<T> deepCopyList(List<T> src) {
        List<T> dest = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            dest = (List<T>) in.readObject();
        } catch (IOException e) {

        } catch (ClassNotFoundException e) {

        }
        return dest;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDialog = new ProgressDialog(getContext());
        mDialog.setMessage(getResources().getString(R.string.wait));
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                //回退键监听信息保存状态
                if (keyCode == KeyEvent.KEYCODE_BACK && isDataChanged()) {
                    saveInfoData();
                    return true;
                }
                return false;
            }
        });
        mDialog.setCanceledOnTouchOutside(false);

        //初始化退出弹窗
        mAlertDialog = new AlertDialog.Builder(getActivity())
                .setMessage(R.string.edt_info_un_save_tips)
                .setNegativeButton(R.string.edt_info_un_save_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        KeyboardUtils.hideSoftInput((Activity) mContext);
                        ((Activity)mContext).finish();
                    }
                })
                .setPositiveButton(R.string.edt_info_un_save_confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        saveInfoData();
                    }
                })
                .create();
        mAlertDialog.setCancelable(true);
        mAlertDialog.setCanceledOnTouchOutside(true);
    }

    @Override
    public void onHideKeyBoard() {
        KeyboardUtils.hideSoftInput((Activity) mContext);
    }

    /**
     * 保存信息
     */
    protected  abstract void saveInfoData();

    /**
     * 信息是否有改动
     *
     * @return
     */
    public abstract boolean isDataChanged();

    /**
     * 退出监听信息保存˙
     */
    public abstract void onExit();

}
